package com.cqw.service;

import com.alibaba.fastjson.JSON;
import com.cqw.constant.RedisKeyConstant;
import com.cqw.domain.Activity;
import com.cqw.domain.LotteryRewardConfig;
import com.cqw.exception.CommonException;
import com.cqw.mapper.one.LotteryRewardConfigMapper;
import com.cqw.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-04-28 11:34
 * @Description:
 */
@Service
@Slf4j
public class LotteryActivityService {

    @Autowired
    private ActivityService activityService;


    @Autowired
    private LotteryRewardConfigMapper lotteryRewardConfigMapper;


    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 查询奖项配置
     *
     * @param activityId
     * @return
     */
    public List<LotteryRewardConfig> getRewardConfigList(Long activityId) {
        String key = RedisKeyConstant.LOTTERY_REWARD_CONFIG + activityId;
        List<LotteryRewardConfig> lotteryRewardConfigs;
        lotteryRewardConfigs = (List<LotteryRewardConfig>) redisTemplate.opsForValue().get(key);
        if (StringUtils.isNull(lotteryRewardConfigs)) {
            LotteryRewardConfig lotteryRewardConfig = new LotteryRewardConfig();
            lotteryRewardConfig.setActivityId(activityId);
            lotteryRewardConfigs = lotteryRewardConfigMapper.selectAllByCondition(lotteryRewardConfig);
            if (CollectionUtils.isEmpty(lotteryRewardConfigs)) {
                return Collections.emptyList();
            }
            redisTemplate.opsForValue().set(key, lotteryRewardConfigs, 10, TimeUnit.MINUTES);
        }
        log.info("activityId:{},奖项：{}", activityId, JSON.toJSONString(lotteryRewardConfigs));
        return lotteryRewardConfigs;
    }


    /**
     * 抽奖
     *
     * @param activityId
     * @return
     */
    public LotteryRewardConfig doLottery(Long activityId) {
        Activity activityInfo = activityService.getActivityInfo(activityId, false);
        if (activityInfo == null) {
            throw new CommonException("活动不存在");
        }

        //奖项
        List<LotteryRewardConfig> rewardConfigList = getRewardConfigList(activityId);
        LotteryRewardConfig lotteryRewardConfig = lotteryByRate(rewardConfigList);
        return lotteryRewardConfig;
    }


    /**
     * 抽奖
     *
     * @param rewardConfigList
     */
    private LotteryRewardConfig lotteryByRate(List<LotteryRewardConfig> rewardConfigList) {
        //生成随机数
        double random = Math.random();
        log.info("产生的随机数是：{}", random);
        double left = 0;
        double right = 0;

        for (LotteryRewardConfig lotteryRewardConfig : rewardConfigList) {
            double rate = Double.parseDouble(lotteryRewardConfig.getRate());
            right += rate;
            if (random > left && random <= right) {
                return lotteryRewardConfig;
            }
            left += rate;
        }
        //没抽中，返回null
        return new LotteryRewardConfig();
    }
}
